package com.lzy.cloud.order.utils;

import org.springframework.expression.ParseException;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @description:公共工具类
 * @author: liyun
 * @create: 2018-07-10 16:01
 */
public class CommonUtil {

    /**
     * 2个日期间隔的天数
     * @param str1
     * @param str2
     * @return
     * @throws Exception
     */
    public static long getDistanceDays(String str1, String str2) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date one;
        Date two;
        long days=0;
        try {
            one = df.parse(str1);
            two = df.parse(str2);
            long time1 = one.getTime();
            long time2 = two.getTime();
            long diff ;
            if(time1<time2) {
                diff = time2 - time1;
            } else {
                diff = time1 - time2;
            }
            days = diff / (1000 * 60 * 60 * 24)+1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return days;
    }
    public static void main(String[] args) throws Exception {
        System.out.println(getDistanceDays("2018-01-01 09:00:00", "2018-01-03 00:00:00"));
        System.out.println( Integer.parseInt("12"));
    }

    /**
     * 日期+1天
     * @param date
     * @return
     * @throws Exception
     */
    public static String addOneDayForDate(String date){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date sDate = null;
        try {
            sDate = sdf.parse(date);
        } catch (java.text.ParseException e) {
            e.printStackTrace();
        }

        Calendar c = Calendar.getInstance();
        c.setTime(sDate);
        c.add(Calendar.DAY_OF_MONTH, 1);
        sDate = c.getTime();

        date = sdf.format(sDate);
        return date;
    }

    /**
     * 时间+1天
     * @param time
     * @return
     * @throws Exception
     */
    public static String addOneDayFotTime(String time){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date sDate = null;
        try {
            sDate = sdf.parse(time);
        } catch (java.text.ParseException e) {
            e.printStackTrace();
        }

        Calendar c = Calendar.getInstance();
        c.setTime(sDate);
        c.add(Calendar.DAY_OF_MONTH, 1);
        sDate = c.getTime();

        time = sdf.format(sDate);
        return time;
    }


}
